package com.bjsxt.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bjsxt.commons.DataGridModel;
import com.bjsxt.pojo.Provider;
import com.bjsxt.service.ProviderService;

/**
 * @author 陈强
 */
@Controller
@RequestMapping("/provider")
public class ProviderController {
	@Autowired
	private ProviderService providerService;
	
	/**
	 * 查询所有供应商
	 * @param page 第几页，默认第一页
	 * @param rows 一页多少条数据
	 * @return 返回一个对应当页条数和信息的list供应商集合
	 */
	@RequestMapping("/showProvider")
	@ResponseBody
	public DataGridModel showProvider(@RequestParam(defaultValue="1") Integer page, @RequestParam(defaultValue="5") Integer rows){
		return this.providerService.findProvider(page,rows);
	}
	
	
	/**
	 * 
	 * @param provider
	 * @return
	 */
	@RequestMapping("/addProvider")
	@ResponseBody
	public Map<String,Integer>addProvider(Provider provider){
		Map<String, Integer>map=new HashMap<>(1);
		try{
			this.providerService.addProvider(provider);
			map.put("state", 200);
			return map;
		}catch(Exception e){
			e.printStackTrace();
			map.put("state",500);
			return map;
		}
	}
	
	/**
	 * 删除供应商
	 * @param 客户端传来的供应商id
	 * @return 返回一个结果响应给客户端
	 */
	@RequestMapping("/dropProvider")
	@ResponseBody
	public Map<String, Integer>  dropProvider(Integer providerid){
		Map<String, Integer>map=new HashMap<>(1);
		System.out.println("asd");
		try{
			this.providerService.dropProviderById(providerid);
			map.put("state", 200);
			return map;
		}catch(Exception e){
			e.printStackTrace();
			map.put("state", 500);
			return map;
		}
	}
	
	/**
	 * 修改供应商信息
	 * @param provider 客户端传来的供应商信息
	 * @return 返回一个结果响应给客户端
	 */
	@RequestMapping("/modifyProvider")
	@ResponseBody
	public Map<String, Integer> modifyProvider(Provider provider){
		Map<String, Integer>map=new HashMap<>(1);
		try{
			this.providerService.modifyProvider(provider);
			map.put("state", 200);
			return map;
		}catch(Exception e){
			e.printStackTrace();
			map.put("state", 500);
			return map;
		}
	}
}
